package views.employer;

import org.intellij.lang.annotations.Language;
import tools.Hint;
import tools.Icons;
import utils.DBUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Statement;

/*ok*/

public class EmployerAddFrame extends JFrame {
    ImageIcon imageIcon = new ImageIcon("src/images/employer.png");
    private JPanel panBtn = new JPanel();
    private JPanel panLab = new JPanel();
    private JLabel jL_employername = new JLabel("用人单位名称：");
    private JLabel jL_contactperson = new JLabel("联系人：");
    private JLabel jL_contactphone = new JLabel("联系电话：");
    private JLabel jL_address = new JLabel("地址：");
    private JTextField jT_employername = new JTextField();//用人单位名称输入框
    private JTextField jT_contact_person = new JTextField();
    private JTextField jT_contactphone = new JTextField();
    private JTextField jT_address = new JTextField();
    private JButton btn_add = new JButton("添加");
    private JButton btn_reset = new JButton("重置");
    private JLabel jL_title = new JLabel();
    Icons icons = new Icons();

    public EmployerAddFrame() {
        setTitle("添加用人单位");
        setSize(400, 300);
        setIconImage(imageIcon.getImage().getScaledInstance(80, 80, Image.SCALE_DEFAULT));
        setResizable(false); // 不可改变窗口大小
        // 获取屏幕大小和当前frame的大小
        Dimension thisScreen = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension thisFrame = this.getSize();
        // 使启动窗口位于屏幕的正中心
        setLocation((thisScreen.width - thisFrame.width) / 2,
                (thisScreen.height - thisFrame.height) / 2);
        // 设置单击窗口的【关闭】按钮时将发生相应的动作
        setDefaultCloseOperation(HIDE_ON_CLOSE);
        jL_title.setFont(new Font("TsangerYuMo W03", Font.BOLD, 26));
        jL_title.setHorizontalAlignment(JLabel.CENTER);
        jL_title.setForeground(new Color(255, 51, 51));
        jL_title.setText("添 加 用 人 单 位");

        jL_employername.setSize(100, 80);
        jL_employername.setLocation(50, 5);
        jL_employername.setHorizontalAlignment(JLabel.RIGHT);

        jL_contactperson.setSize(100, 80);
        jL_contactperson.setLocation(50, 40);
        jL_contactperson.setHorizontalAlignment(JLabel.RIGHT);

        jL_contactphone.setSize(100, 80);
        jL_contactphone.setLocation(50, 80);
        jL_contactphone.setHorizontalAlignment(JLabel.RIGHT);

        jL_address.setSize(100, 80);
        jL_address.setLocation(50, 120);
        jL_address.setHorizontalAlignment(JLabel.RIGHT);

        /*文本框位置*/
        jT_employername.setSize(150, 25);
        jT_employername.setLocation(160, 33);
        String hint_name = "输入用人单位名称";
        jT_employername.addFocusListener(new Hint(jT_employername, hint_name));

        jT_contact_person.setSize(150, 25);
        jT_contact_person.setLocation(160, 68);
        String hint_person = "输入联系人";
        jT_contact_person.addFocusListener(new Hint(jT_contact_person, hint_person));

        jT_contactphone.setSize(150, 25);
        jT_contactphone.setLocation(160, 108);
        String hint_phone = "输入联系电话";
        jT_contactphone.addFocusListener(new Hint(jT_contactphone, hint_phone));

        jT_address.setSize(150, 25);
        jT_address.setLocation(160, 148);
        String hint_address = "输入地址";
        jT_address.addFocusListener(new Hint(jT_address, hint_address));

        panBtn.add(btn_add);
        panBtn.add(btn_reset);
        panLab.setLayout(null);

        // panLab.add(jLabel1);
        panLab.add(jL_employername);
        panLab.add(jL_contactperson);
        panLab.add(jL_contactphone);
        panLab.add(jL_address);

        panLab.add(jT_employername);
        panLab.add(jT_contact_person);
        panLab.add(jT_contactphone);
        panLab.add(jT_address);

        add(jL_title, BorderLayout.NORTH);
        add(panBtn, BorderLayout.SOUTH);
        add(panLab, BorderLayout.CENTER);

        btn_add.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                String employername = jT_employername.getText().trim();
                String contactperson = jT_contact_person.getText().trim();
                String contactphone = jT_contactphone.getText().trim();
                String address = jT_address.getText().trim();

                if (employername.equals(hint_name) || contactperson.equals(hint_person) || contactphone.equals(hint_phone) || address.equals(hint_address)) {
                    JOptionPane.showMessageDialog(null, "请输入内容", "错误", JOptionPane.WARNING_MESSAGE, icons.getQuestionImage());
                } else {
                    Statement stmt = DBUtil.getStatement();
                    @Language("MySQL")
                    String sql2 = "insert into employer(employer_name, contact_person, contact_phone, address) values( '"
                            + employername
                            + "' , '"
                            + contactperson
                            + "' , '"
                            + contactphone
                            + "' , '"
                            + address
                            + "' );";
                    try {
                        stmt.executeUpdate(sql2);
                        JOptionPane.showMessageDialog(null, "添加成功。");
                        dispose();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        btn_reset.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                jT_employername.setText("");
                jT_contact_person.setText("");
                jT_contactphone.setText("");
                jT_address.setText("");
            }
        });
    }
}
